<?php
class model__com_model extends database_mysql
{
    public function __construct()
    {
        parent::__construct();
        
    }
    
    public function md_cm_list()
    {
        $sql = "
                SELECT *
                FROM
                ".prefix."categories
        ";
        $rs = parent::mysql_fetch_all_rows($sql);
        return $rs;
    }
    
    public function md_cm_list_com(){
        $del_show = "AND ".prefix."categories_lang.`Status` <> 'del'";
        if($_SESSION['ID'] == 1){
            $del_show = '';
        }
        $sql = "
                SELECT
                ".prefix."categories.ID,
                ".prefix."categories_lang.`Status`,
                ".prefix."categories.Com
                FROM
                ".prefix."categories
                INNER JOIN ".prefix."categories_lang ON ".prefix."categories.ID = ".prefix."categories_lang.CatID
                WHERE
                ".prefix."categories.Define = 'com' $del_show
                GROUP BY
                ".prefix."categories.ID

        ";
        $rs = parent::mysql_fetch_all_rows($sql);
        return $rs;
    }
    
    public function md_cm_get_lang(){
        
        $select = " 
                    SELECT
                    ". prefix ."language.ID,
                    ". prefix ."language.Lang,
                    ". prefix ."language.DirName,
                    ". prefix ."language.`Code`
                    FROM
                    ". prefix ."language
                    WHERE
                    ". prefix ."language.`Status` = 'true'
        ";
        return parent::mysql_fetch_all_rows($select);
    }
    
    public function md_cm_insert_com($com_name, $app){
        $sql = "
                    SELECT
                    prefix_categories.Com
                    FROM
                    prefix_categories
                    INNER JOIN prefix_categories_lang ON prefix_categories.ID = prefix_categories_lang.CatID
                    WHERE
                    prefix_categories.Define = 'com' AND
                    prefix_categories.Com = '$com_name'
        ";
        $result = parent::mysql_fetch_all_rows($sql);
        if(empty($result)){
            
            $data = array(
                'Com' => $com_name,
                'App' => $app,
                'Define' => 'com',
                'Act' => 1,
                'PID' => 1
                
            );
            $id_last = $this->inser_db('categories', $data);
            $list_lang = $this->md_cm_get_lang();
            if(!empty($list_lang) && $id_last >0){
                foreach($list_lang as $value){
                    $data = array();
                    $data['SeoKey'] = $_POST['seokey_name_'.$value['Code']];
                    $data['CatName'] = $_POST['cat_name_'.$value['Code']];
                    $data['Status'] = $_POST['status_'.$value['Code']];
                    $data['LangID'] = $value['ID'];
                    $data['CatID'] = $id_last;
                    $result = $this->inser_db('categories_lang', $data);
                }
            }
        }
        
    }
    
    public function md_cm_update_com(){
        $list_lang = $this->md_cm_get_lang();
        if(!empty($list_lang)){
            foreach($list_lang as $value){
                $data = array();
                $data['SeoKey'] = $this->replace_string($_POST['seokey_name_'.$value['Code']]);
                $data['CatName'] = $this->replace_string($_POST['cat_name_'.$value['Code']]);
                $data['Status'] = $_POST['status_'.$value['Code']];
                $data['LangID'] = $value['ID'];
                $id= $_POST['id'];
                
                $table = prefix . 'categories_lang';
                $condition = 'CatID = ' . $id . ' AND '.prefix.'categories_lang.LangID = ' . $value['ID']; 
                
                $check_com_id = $this->md_cm_check_com($id,$value['ID']);
                if(!empty($check_com_id)){
                    parent::mysql_update_array($table, $data, $condition);
                }else{
                    $data['CatID'] = $id;
                    echo 1;
                    $result = $this->inser_db('categories_lang', $data);
                }
                
            }
        }
    }
    
    public function inser_db($table, $data){
        $insKey = array();
        $insVal = array();
        foreach($data as $k => $v){
            $v = parent::mysql_check_string($v);
            array_push($insKey,"`".$k."`");
            array_push($insVal,"'".$v."'");
        }
        //echo "INSERT INTO ". prefix ."$table (".implode(',',$insKey).") VALUES (".implode(',',$insVal).")". '<BR>';
        $this->mysql_update_insert("INSERT INTO ". prefix ."$table (".implode(',',$insKey).") VALUES (".implode(',',$insVal).")");
        $id_last =  mysql_insert_id();
        return $id_last;
    }
    
    public function  md_cm_update_status($data, $condition){
        $v = '';
        foreach($data as $key=>$value){
            $v[] = "`$key` = '$value'";
        }    
        $sql = "UPDATE `". prefix ."categories_lang` SET ".implode(',', $v)." WHERE `". prefix ."categories_lang`.`CatID` IN($condition)";
        parent::mysql_update_insert($sql);
    }
    
    public function md_cm_get_com($id, $langID){
        $select = " 
                    SELECT
                    ". prefix ."categories_lang.SeoKey,
                    ". prefix ."categories_lang.CatName,
                    ". prefix ."categories_lang.`Status`,
                    ". prefix ."categories.ID,
                    ". prefix ."categories_lang.LangID,
                    ". prefix ."categories.App
                    FROM
                    ". prefix ."categories
                    INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories_lang.CatID = ". prefix ."categories.ID
                    WHERE
                    ". prefix ."categories.ID = $id AND
                    ". prefix ."categories.Define = 'com' AND
                    ". prefix ."categories_lang.LangID = $langID
        ";
        return parent::mysql_fetch_one_rows($select);
    }
    
    public function md_cm_check_com($id, $lang_is){
        $select = " 
                    SELECT
                    ". prefix ."categories_lang.CatID
                    FROM
                    ". prefix ."categories_lang
                    INNER JOIN ". prefix ."categories ON ". prefix ."categories_lang.CatID = ". prefix ."categories.ID
                    WHERE
                    ". prefix ."categories_lang.LangID = $lang_is AND
                    ". prefix ."categories_lang.CatID = $id AND
                    ". prefix ."categories.Define = 'com'

        ";
        return parent::mysql_fetch_one_rows($select);
    }
    
    public function md_cm_categories_exit_com($com){
        $select = " 
                    SELECT
                    ". prefix ."categories.Com
                    FROM
                    ". prefix ."categories
                    WHERE
                    ". prefix ."categories.Com = '$com' AND
                    ". prefix ."categories.Define = 'com'


        ";
        return parent::mysql_fetch_one_rows($select);
    }
    
    public function md_cm_check_del($id){
        $select = " 
                    SELECT
                    ". prefix ."categories_lang.SeoKey
                    FROM
                    ". prefix ."categories_lang
                    WHERE
                    ". prefix ."categories_lang.`Status` = 'del' AND
                    ". prefix ."categories_lang.CatID = $id


        ";
        return parent::mysql_fetch_one_rows($select);
    }
    
    public function md_cm_delItem($id){
        $table = 'categories_lang';
        parent::mysql_delete("DELETE FROM `". prefix ."$table` WHERE `". prefix ."$table`.`CatID` IN($id)");
        $table = 'categories';
        parent::mysql_delete("DELETE FROM `". prefix ."$table` WHERE `". prefix ."$table`.`ID` IN($id)");
    }
    
    public function replace_string($string){
        return preg_replace('/[^a-zA-Z0-9\-]/s', '', $String);
    }
}
